﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using log4net.Repository.Hierarchy;

namespace EasyStep.Common
{   
    public class Logger
    {
        private ILog _Log; 
        protected string LogFolderPath { get; set; }
        //public ILog Log { get 
        //{
        //    object locker = new object();
        //    lock(locker)
        //    {
        //        Configure();
        //    }
        //    return _Log;
        //} }
        
        public Logger(string logFolderPath)
        {
            XmlConfigurator.Configure();
            _Log = LogManager.GetLogger(Guid.NewGuid().ToString());
            LogFolderPath = logFolderPath;
        }
        protected void Configure()
        {
            if (_Log != null)
            {
                FileAppender rootAppender = (FileAppender)((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Appenders[0];
                rootAppender.File = string.Format("{0}\\{1}", LogFolderPath, "log.txt");
                rootAppender.ActivateOptions();
            }
        }
        public void Warn(string message)
        {
            Configure();
            _Log.Warn(message);
        }
        public void Error(string message)
        {
            Configure();
            _Log.Error(message);
        }
        public void Info(string message)
        {
            Configure();
            _Log.Info(message);
        }
    }
}
